/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package at.irian.demo.jsfatwork.service;

import at.irian.demo.jsfatwork.domain.Answer;
import at.irian.demo.jsfatwork.domain.Poll;
import at.irian.demo.jsfatwork.domain.User;
import at.irian.demo.jsfatwork.domain.Vote;
import at.irian.demo.jsfatwork.repository.GenericRepository;
import at.irian.demo.jsfatwork.repository.VotingRepository;
import at.irian.demo.jsfatwork.service.exception.ServiceException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.inject.Inject;
import java.util.List;

/**
 * @author JSF@Work Project Team
 */
@Service
public class DefaultVotingService extends AbstractGenericService<Vote> implements VotingService
{
    private VotingRepository votingRepository;

    //needed for creating proxies
    protected DefaultVotingService()
    {
    }

    @Inject
    public DefaultVotingService(VotingRepository votingRepository)
    {
        this.votingRepository = votingRepository;
    }

    protected GenericRepository<Vote> getRepository()
    {
        return this.votingRepository;
    }

    @Transactional
    public boolean vote(User votedBy, Poll poll, List<Answer> answers)
    {
        if (!isVoteValid(votedBy, poll, answers))
        {
            return false;
        }

        Vote vote = new Vote(poll, votedBy);
        vote.setActive(true);

        for (Answer answer : answers)
        {
            vote.addAnswer(answer);
        }

        this.votingRepository.save(vote);
        return true;
    }

    private boolean isVoteValid(User votedBy, Poll poll, List<Answer> answers)
    {
        if (votedBy == null || poll == null || answers == null || answers.isEmpty())
        {
            return false;
        }

        if(!poll.isMultipleAnswerMode() && answers.size() > 1)
        {
            return false;
        }
        return true;
    }

    @Override
    @Transactional
    public void save(Vote vote)
    {
        if(!isVoteValid(vote))
        {
            throw new ServiceException("violation_vote_failed");
        }
        super.save(vote);
    }

    private boolean isVoteValid(Vote vote)
    {
        if(vote == null)
        {
            return false;
        }

        if(vote.getAnswers() == null ||vote.getAnswers().isEmpty() ||
                (!vote.getPoll().isMultipleAnswerMode() && vote.getAnswers().size() > 1))
        {
            return false;
        }

        return true;
    }

    @Override
    public List<Vote> findVotes(User user)
    {
        return this.votingRepository.loadVotes(user);
    }
}